package cv.vender.http;


import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.util.Base64;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;

import cv.vender.clsArgs;
import cv.vender.clsPublicPara;
import cv.vender.clsToolBox;
import cv.vender.http.bean.clsSendTradeToPc;
import cv.vender.http.bean.clsVenderBean;
import cv.vender.para.clsProductPara;

public class clsHttpFrame {

    public static final int DATA_TYPE_BYTES = 1;
    public static final int DATA_TYPE_STRING = 0;
    /*发送货道信息报文*/
    private static final int SLOT_SIZE = 11;
    private static int FrameId;
    private int cmd;
    private int Datatype;
    private int vid;
    private String Data_str;
    private byte[] Data_byte;
    private int fid;

    /*发送心跳信息*/
    public static void SendHttpVenderFrame() {
        clsArgs args = clsArgs.getInst();
        Handler handler = clscvhttp.getInstance().getmChildHandler();
        Message msg = handler.obtainMessage();
        clsVenderBean para = clsPublicPara.VenderBean;
        //{"Type":"VENDER","Vid":2001,"CHGEStat":0,"BILLStat":3,"IRErrCnt":0,"LstSltE":126004,"CoinCnt":0,"BllCnt":0,"Ver":373}

        //para.setVer(clsPublicPara.Vercode);

        clsHttpFrame frame = new clsHttpFrame();
        frame.setVid(args.getVenderid());
        frame.setDatatype(DATA_TYPE_STRING);
        frame.setData_str(para.ToJsonObject().toString());
        frame.setCmd(clscvhttp.HTTP_CMD_VENDER);

        msg.obj = frame;
        msg.what = clscvhttp.HTTP_CMD_VENDER;
        handler.sendMessage(msg);
    }

    /*发送获取商品信息报文*/
    public static void SendHttpGoodsRequestFrame(int page) {
        clsArgs args = clsArgs.getInst();
        Handler handler = clscvhttp.getInstance().getmChildHandler();
        Message msg = handler.obtainMessage();
        JSONObject json = new JSONObject();

        try {
            json.put("Type", "GOODS");
            json.put("Vid", args.getVenderid());
            json.put("Page", page);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        clsHttpFrame frame = new clsHttpFrame();
        frame.setVid(args.getVenderid());
        frame.setDatatype(DATA_TYPE_STRING);
        frame.setData_str(json.toString());
        frame.setCmd(clscvhttp.HTTP_CMD_GOODS);

        msg.obj = frame;
        msg.what = frame.getCmd();
        handler.sendMessage(msg);
    }

    /**
     * 获取服务器时间
     */
    public static void SendHttpTimeRequestFrame() {
        clsArgs args = clsArgs.getInst();
        Handler handler = clscvhttp.getInstance().getmChildHandler();
        Message msg = handler.obtainMessage();
        JSONObject json = new JSONObject();

        try {
            json.put("Type", "TIME");
        } catch (JSONException e) {

            e.printStackTrace();
        }

        clsHttpFrame frame = new clsHttpFrame();
        frame.setVid(args.getVenderid());
        frame.setDatatype(DATA_TYPE_STRING);
        frame.setData_str(json.toString());
        frame.setCmd(clscvhttp.HTTP_CMD_SYNC_TIME);

        msg.obj = frame;
        msg.what = frame.getCmd();
        handler.sendMessage(msg);
    }

    //发送位置和时间
    public static void SendHttpSlotSendFrame() {
        clsArgs args = clsArgs.getInst();
        Handler handler = clscvhttp.getInstance().getmChildHandler();
        Message msg = handler.obtainMessage();

        ArrayList<clsProductPara> goodslst = clsPublicPara.productlst;
        byte[] data = new byte[5 + goodslst.size() * SLOT_SIZE];
        int i = 0;

        data[i++] = (byte) 'S';
        clsToolBox.intToByte(args.getVenderid(), data, i);
        i += 4;

        for (clsProductPara goodsroad : goodslst) {
            clsToolBox.shortToByte((short) goodsroad.getId(), data, i);
            i += 2;
            data[i] = (byte) 0;
            i++;
            data[i] = (byte) 1;
            i++;
            data[i] = (byte) 1;
            i++;
            clsToolBox.intToByte(goodsroad.getPrice(), data, i);
            i += 4;
            clsToolBox.shortToByte((short) goodsroad.getId(), data, i);
            i += 2;
        }

        clsHttpFrame frame = new clsHttpFrame();
        frame.setVid(args.getVenderid());
        frame.setDatatype(DATA_TYPE_BYTES);
        frame.setData_byte(data);
        frame.setCmd(clscvhttp.HTTP_CMD_SLOT);

        msg.obj = frame;
        msg.what = frame.getCmd();
        handler.sendMessage(msg);
    }

    /**
     * 获取二维码
     *
     * @param goodroadid 货道编号
     * @param price      价格
     * @param goodscount 商品剩余数量
     */
    public static void SendHttpQrcodeRequestFrame(int goodroadid, int price, int goodscount, int pay_type)

    {
        clsArgs args = clsArgs.getInst();
        Handler handler = clscvhttp.getInstance().getmChildHandler();
        Message msg = handler.obtainMessage();
        JSONObject json = new JSONObject();

        try {
            json.put("Type", "REQQR");
            json.put("SId", goodroadid);
            json.put("PRICE", price);
            json.put("CNT", goodscount);
            json.put("PTTYP", pay_type);
            json.put("VTIME", 3600 + 120);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        clsHttpFrame frame = new clsHttpFrame();
        frame.setVid(args.getVenderid());
        frame.setDatatype(DATA_TYPE_STRING);
        frame.setData_str(json.toString());
        frame.setCmd(clscvhttp.HTTP_CMD_QRCODE);

        msg.obj = frame;
        msg.what = frame.getCmd();
        handler.sendMessage(msg);
    }

    /**
     * 发送交易数据到服务器
     *
     * @param tradeToPc
     */
    public static void SendHttpTradeFrame(clsSendTradeToPc tradeToPc) {

        //clsArgs args=clsArgs.getInst();
        Handler handler = clscvhttp.getInstance().getmChildHandler();
        Message msg = handler.obtainMessage();
        JSONObject json = new JSONObject();

        try {
            json.put("Type", "TRADE");
            json.put("Vid", tradeToPc.getVendId());
            json.put("BILLS", tradeToPc.getPutBills());
            json.put("CHANEGS", 0);
            json.put("COINS", 0);
            json.put("ISOK", (tradeToPc.getErr() == 0) ? 1 : 0);

            json.put("PAY_TYPE", tradeToPc.getPayTyp());
            json.put("Price", tradeToPc.getCashAmount());
            json.put("RESTCASH", 0);
            json.put("SErr", tradeToPc.getErr());
            json.put("SId", tradeToPc.getSlotid());

            json.put("TIME", tradeToPc.getErr());

            json.put("TId", tradeToPc.getLiushuiID());
            json.put("TIME", clsToolBox.getTimeLongString(new Date(System.currentTimeMillis())));
            json.put("ORDRID", tradeToPc.getTradeId());
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        clsHttpFrame frame = new clsHttpFrame();
        frame.setVid(tradeToPc.getVendId());
        frame.setDatatype(DATA_TYPE_STRING);
        frame.setData_str(json.toString());
        frame.setCmd(clscvhttp.HTTP_CMD_TRADE);

        msg.obj = frame;
        msg.what = frame.getCmd();
        msg.sendToTarget();
    }

    /**
     * 获取货道编号
     *
     * @param goodroadid 货道编号
     */
    public static void SendHttpSlotRequestFrame(int goodroadid) {
        clsArgs args = clsArgs.getInst();
        Handler handler = clscvhttp.getInstance().getmChildHandler();
        Message msg = handler.obtainMessage();
        JSONObject json = new JSONObject();

        try {
            json.put("Type", "SLOT");
            json.put("SId", goodroadid);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        clsHttpFrame frame = new clsHttpFrame();
        frame.setVid(args.getVenderid());
        frame.setDatatype(DATA_TYPE_STRING);
        frame.setData_str(json.toString());
        frame.setCmd(clscvhttp.HTTP_CMD_REQUEST_SLOT);

        msg.obj = frame;
        msg.what = frame.getCmd();
        handler.sendMessage(msg);
    }

    //发送请求获取订单信息
    public static void SendHttpPollFrame() {
        clsArgs args = clsArgs.getInst();
        Handler handler = clscvhttp.getInstance().getmChildHandler();
        Message msg = handler.obtainMessage();
        JSONObject json = new JSONObject();

        try {
            json.put("Type", "HT");
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        clsHttpFrame frame = new clsHttpFrame();
        frame.setVid(args.getVenderid());
        frame.setDatatype(DATA_TYPE_STRING);
        frame.setData_str(json.toString());
        frame.setCmd(clscvhttp.HTTP_CMD_HEART);

        msg.obj = frame;
        msg.what = frame.getCmd();
        handler.sendMessage(msg);
    }

    //网络请求生成交易请求
    public static void SendHttpSwipeTradeRequest(clsSendTradeToPc para) {
        //{"Type":"VSWIP","CARD":"12345678","PRICE":1000,"PARA":12,"Vid":1,"CNT":2}
        Handler handler = clscvhttp.getInstance().getmChildHandler();
        Message msg = handler.obtainMessage();
        JSONObject json = new JSONObject();

        try {
            json.put("Type", "VSWIP");
            json.put("CARD", para.getUser());
            json.put("PRICE", para.getCashAmount());
            json.put("PARA", para.getSlotid());
            json.put("Vid", para.getVendId());
            json.put("CNT", 1);

        } catch (JSONException e) {
            e.printStackTrace();
        }

        clsHttpFrame frame = new clsHttpFrame();
        frame.setVid(para.getVendId());
        frame.setDatatype(DATA_TYPE_STRING);
        frame.setData_str(json.toString());
        frame.setCmd(clscvhttp.HTTP_CMD_MAKE_SWIPE_TRADE);

        msg.obj = frame;
        msg.what = frame.getCmd();
        handler.sendMessage(msg);
    }

    //发送网络交易时间
    public static void SendHttpTradeFrame(String line) {
        clsArgs args = clsArgs.getInst();
        clsHttpFrame frame = new clsHttpFrame();
        frame.setVid(args.getVenderid());
        frame.setDatatype(DATA_TYPE_STRING);
        frame.setData_str(line);
        frame.setCmd(clscvhttp.HTTP_CMD_TRADE);
        Handler handler = clscvhttp.getInstance().getmChildHandler();
        Message msg = handler.obtainMessage();

        msg.obj = frame;
        msg.what = frame.getCmd();
        handler.sendMessage(msg);
    }

    public int getDatatype() {
        return Datatype;
    }

    public void setDatatype(int datatype) {
        Datatype = datatype;
    }

    public int getVid() {
        return vid;
    }

    public void setVid(int vid) {
        this.vid = vid;
    }

    public String getData_str() {
        return Data_str;
    }

    public void setData_str(String data_str) {
        Data_str = data_str;
    }

    public byte[] getData_byte() {
        return Data_byte;
    }

    public void setData_byte(byte[] data_byte) {
        Data_byte = data_byte;
    }

    public int getCmd() {
        return cmd;
    }

    public void setCmd(int cmd) {
        this.cmd = cmd;
    }

    @SuppressLint("DefaultLocale")
    public String ToBase64String() {
        String str = null;

        FrameId++;
        this.fid = FrameId;
        if (Datatype == 1) {
            /*字节类型*/
            str = String.format("t=5&g=20&f=%d&m=%d&p=%s", FrameId, vid, Base64.encodeToString(Data_byte, Base64.DEFAULT | Base64.NO_WRAP));
        } else {
            /*字符串类型*/
            try {
                str = String.format("t=5&g=20&f=%d&m=%d&p=%s", FrameId, vid, Base64.encodeToString(Data_str.getBytes("GBK"), Base64.DEFAULT | Base64.NO_WRAP));
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return str;
    }

    public int getFid() {
        return fid;
    }

    public void setFid(int fid) {
        this.fid = fid;
    }
}
